package cn.ai.shoes.client.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Map;

@Component
public class ComfyuiServerUtil {
    private final ComfyuiServerProperties properties;

    @Autowired
    public ComfyuiServerUtil(ComfyuiServerProperties properties) {
        this.properties = properties;
    }

    /**
     * 通过 key 获取 comfyui 服务器 url
     */
    public String getUrlByKey(String key) {
        return properties.getUrls().get(key);
    }

    /**
     * 获取所有 comfyui 服务器的 map
     */
    public Map<String, String> getAllServerUrls() {
        return properties.getUrls();
    }

    /**
     * 通过 value（url）获取 comfyui 服务器的 key
     */
    public String getKeyByUrl(String url) {
        if (url == null) return null;

        for (Map.Entry<String, String> entry : properties.getUrls().entrySet()) {
            if (url.equals(entry.getValue())) {
                return entry.getKey();
            }else if (url.equals(entry.getValue().substring(0,entry.getValue().length()-1))){
                return entry.getKey();
            }
        }
        return null;
    }
}
